home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 701-725 / 708 / intuisup / intuisup42.lha / Intuisup / source.lha / Editor / subs.c < prev    next >
C/C++ Source or Header  |  1992-01-02  |  8KB  |  325 lines

  1. /* $Revision Header *** Header built automatically - do not edit! ***********
  2.  *
  3.  *    (C) Copyright 1991 by Torsten Jürgeleit
  4.  *
  5.  *    Name .....: subs.c
  6.  *    Created ..: Sunday 22-Dec-91 21:23:09
  7.  *    Revision .: 0
  8.  *
  9.  *    Date        Author                 Comment
  10.  *    =========   ====================   ====================
  11.  *    22-Dec-91   Torsten Jürgeleit      Created this file!
  12.  *
  13.  ****************************************************************************
  14.  *
  15.  *    Subroutines
  16.  *
  17.  * $Revision Header ********************************************************/
  18.  
  19.     /* Includes */
  20.  
  21. #include "includes.h"
  22. #include "defines.h"
  23. #include "imports.h"
  24. #include "protos.h"
  25.  
  26.     /* Draw box in given window */
  27.  
  28.    VOID
  29. draw_box(struct Window  *win, struct Box  *box)
  30. {
  31.    struct RastPort  *rp = win->RPort;
  32.  
  33.    SetDrMd(rp, (LONG)COMPLEMENT);
  34.    Move(rp, (LONG)box->bo_X1, (LONG)box->bo_Y1);
  35.    Draw(rp, (LONG)box->bo_X2, (LONG)box->bo_Y1);
  36.    Draw(rp, (LONG)box->bo_X2, (LONG)box->bo_Y2);
  37.    Draw(rp, (LONG)box->bo_X1, (LONG)box->bo_Y2);
  38.    Draw(rp, (LONG)box->bo_X1, (LONG)box->bo_Y1);
  39. }
  40.     /* Draw box in given window with border of 1 pixel */
  41.  
  42.    VOID
  43. draw_box_with_border(struct Window  *win, struct Box  *box)
  44. {
  45.    struct RastPort  *rp = win->RPort;
  46.  
  47.    SetDrMd(rp, (LONG)COMPLEMENT);
  48.    Move(rp, (LONG)(box->bo_X1 - 1), (LONG)(box->bo_Y1 - 1));
  49.    Draw(rp, (LONG)(box->bo_X2 + 1), (LONG)(box->bo_Y1 - 1));
  50.    Draw(rp, (LONG)(box->bo_X2 + 1), (LONG)(box->bo_Y2 + 1));
  51.    Draw(rp, (LONG)(box->bo_X1 - 1), (LONG)(box->bo_Y2 + 1));
  52.    Draw(rp, (LONG)(box->bo_X1 - 1), (LONG)(box->bo_Y1 - 1));
  53. }
  54.     /* Draw box in given window with offset */
  55.  
  56.    VOID
  57. draw_box_with_offset(struct Window  *win, struct Box  *box, SHORT xoffset,
  58.                                   SHORT yoffset)
  59. {
  60.    struct RastPort  *rp = win->RPort;
  61.  
  62.    SetDrMd(rp, (LONG)COMPLEMENT);
  63.    Move(rp, (LONG)(box->bo_X1 + xoffset), (LONG)(box->bo_Y1 + yoffset));
  64.    Draw(rp, (LONG)(box->bo_X2 + xoffset), (LONG)(box->bo_Y1 + yoffset));
  65.    Draw(rp, (LONG)(box->bo_X2 + xoffset), (LONG)(box->bo_Y2 + yoffset));
  66.    Draw(rp, (LONG)(box->bo_X1 + xoffset), (LONG)(box->bo_Y2 + yoffset));
  67.    Draw(rp, (LONG)(box->bo_X1 + xoffset), (LONG)(box->bo_Y1 + yoffset));
  68. }
  69.     /* Get head of EXEC list */
  70.  
  71.    VOID *
  72. get_head(struct List  *list)
  73. {
  74.    struct Node  *node = list->lh_Head;
  75.  
  76.    /* Check for empty list ? */
  77.    if (!node->ln_Succ) {
  78.       node = NULL;
  79.    }
  80.    return((VOID *)node);
  81. }
  82.     /* Get tail of EXEC list */
  83.  
  84.    VOID *
  85. get_tail(struct List  *list)
  86. {
  87.    struct Node  *node = list->lh_TailPred;
  88.  
  89.    /* Check for empty list ? */
  90.    if (!node->ln_Pred) {
  91.       node = NULL;
  92.    }
  93.    return((VOID *)node);
  94. }
  95.     /* Get next node of EXEC list entry */
  96.  
  97.    VOID *
  98. get_succ(struct Node  *node)
  99. {
  100.    /* Check for end of list ? */
  101.    node = node->ln_Succ;
  102.    if (!node->ln_Succ) {
  103.       node = NULL;
  104.    }
  105.    return((VOID *)node);
  106. }
  107.     /* Get previous node of EXEC list entry */
  108.  
  109.    VOID *
  110. get_pred(struct Node  *node)
  111. {
  112.    /* Check for start of list ? */
  113.    node = node->ln_Pred;
  114.    if (!node->ln_Pred) {
  115.       node = NULL;
  116.    }
  117.    return((VOID *)node);
  118. }
  119.     /* Get node from list by given number */
  120.  
  121.    VOID *
  122. get_node(struct List  *list, USHORT num)
  123. {
  124.    struct Node  *node;
  125.  
  126.    if (node = get_head(list)) {
  127.       while (num-- && (node = get_succ(node)));
  128.    }
  129.    return((VOID *)node);
  130. }
  131.     /* Duplicate text string */
  132.  
  133.    SHORT
  134. duplicate_string(BYTE *text, BYTE **ptr)
  135. {
  136.    LONG  len;
  137.    SHORT status = EDITOR_STATUS_NORMAL;
  138.  
  139.    if (!text || (len = strlen(text) + 1) == 1) {
  140.       *ptr = NULL;
  141.    } else {
  142.       if (!(*ptr = DosAllocMem(len))) {
  143.      status = EDITOR_ERROR_OUT_OF_MEM;
  144.       } else {
  145.      CopyMem(text, *ptr, len);
  146.       }
  147.    }
  148.    return(status);
  149. }
  150.     /* Convert string to lower case */
  151.  
  152.    VOID
  153. lower_string(BYTE *text, BYTE *ptr)
  154. {
  155.    if (ptr && text) {
  156.       BYTE c;
  157.  
  158.       while ((c = *text++) != '\0') {
  159.      *ptr++ = tolower(c);
  160.       }
  161.       *ptr = '\0';
  162.    }
  163. }
  164.     /* Convert string to upper case */
  165.  
  166.    VOID
  167. upper_string(BYTE *text, BYTE *ptr)
  168. {
  169.    if (ptr && text) {
  170.       BYTE c;
  171.  
  172.       while ((c = *text++) != '\0') {
  173.      *ptr++ = toupper(c);
  174.       }
  175.       *ptr = '\0';
  176.    }
  177. }
  178.     /* Duplicate text list */
  179.  
  180.    SHORT
  181. duplicate_text_list(struct Template  *old_tp, struct Template  *new_tp)
  182. {
  183.    struct Node  *node;
  184.    SHORT status = EDITOR_STATUS_NORMAL;
  185.  
  186.    NewList(&new_tp->tp_TextList);
  187.    for (node = get_head(&old_tp->tp_TextList); node &&
  188.             status == EDITOR_STATUS_NORMAL; node = get_succ(node)) {
  189.       status = add_template_text_list_entry(new_tp, node->ln_Name);
  190.    }
  191.    return(status);
  192. }
  193.     /* Build template text list from given text array */
  194.  
  195.    SHORT
  196. build_template_text_list(struct Template  *tp, BYTE **text_array)
  197. {
  198.    BYTE  *text;
  199.    SHORT status = EDITOR_STATUS_NORMAL;
  200.  
  201.    NewList(&tp->tp_TextList);
  202.    for (text = *text_array; text && status == EDITOR_STATUS_NORMAL;
  203.                              text = *++text_array) {
  204.       status = add_template_text_list_entry(tp, text);
  205.    }
  206.    if (status != EDITOR_STATUS_NORMAL) {
  207.       free_template_text_list(tp);
  208.    }
  209.    return(status);
  210. }
  211.     /* Build text list entry */
  212.  
  213.    struct Node *
  214. build_text_list_entry(BYTE *text)
  215. {
  216.    struct Node  *node;
  217.  
  218.    if (node = AllocMem((LONG)sizeof(struct Node), (LONG)MEMF_PUBLIC |
  219.                                   MEMF_CLEAR)) {
  220.       if (!(node->ln_Name = DosAllocMem((LONG)(strlen(text) + 1)))) {
  221.      FreeMem(node, (LONG)sizeof(struct Node));
  222.      node = NULL;
  223.       } else {
  224.      strcpy(node->ln_Name, text);
  225.       }
  226.    }
  227.    return(node);
  228. }
  229.     /* Add text entry to template text list */
  230.  
  231.    SHORT
  232. add_template_text_list_entry(struct Template  *tp, BYTE *text)
  233. {
  234.    struct Node  *node;
  235.    SHORT status = EDITOR_STATUS_NORMAL;
  236.  
  237.    if (!(node = build_text_list_entry(text))) {
  238.       status = EDITOR_ERROR_OUT_OF_MEM;
  239.    } else {
  240.       AddTail(&tp->tp_TextList, node);
  241.    }
  242.    return(status);
  243. }
  244.     /* Free template text list */
  245.  
  246.    VOID
  247. free_template_text_list(struct Template  *tp)
  248. {
  249.    struct List  *list = &tp->tp_TextList;
  250.    struct Node  *node;
  251.  
  252.    while (node = RemHead(list)) {
  253.       free_text_list_entry(node);
  254.    }
  255. }
  256.     /* Free text list entry */
  257.  
  258.    VOID
  259. free_text_list_entry(struct Node  *node)
  260. {
  261.    DosFreeMem(node->ln_Name);
  262.    FreeMem(node, (LONG)sizeof(struct Node));
  263. }
  264.     /* Build template text array from template text list */
  265.  
  266.    SHORT
  267. build_template_text_array(struct Template  *tp)
  268. {
  269.    struct List  *list = &tp->tp_TextList;
  270.    struct Node  *node;
  271.    BYTE   **text_array;
  272.    USHORT count = 0;
  273.    SHORT  status = EDITOR_STATUS_NORMAL;
  274.  
  275.    /* Count entries in text list */
  276.    for (node = get_head(list); node; node = get_succ(node)) {
  277.       count++;
  278.    }
  279.    if (!(text_array = DosAllocMem((LONG)(count + 1) * sizeof(BYTE *)))) {
  280.       status = EDITOR_ERROR_OUT_OF_MEM;
  281.    } else {
  282.       struct GadgetData  *gd = &tp->tp_Data.tp_GadgetData;
  283.  
  284.       switch (tp->tp_Type) {
  285.      case TEMPLATE_TYPE_MX :
  286.         gd->gd_SpecialData.gd_MXData.gd_MXTextArray = text_array;
  287.         break;
  288.      case TEMPLATE_TYPE_CYCLE :
  289.         gd->gd_SpecialData.gd_CycleData.gd_CycleTextArray = text_array;
  290.         break;
  291.       }
  292.  
  293.       /* Fill text array with pointers to text list entries */
  294.       for (node = get_head(list); node && status == EDITOR_STATUS_NORMAL;
  295.                             node = get_succ(node)) {
  296.      *text_array++ = node->ln_Name;
  297.       }
  298.       *text_array = NULL;   /* mark end of text array */
  299.    }
  300.    return(status);
  301. }
  302.     /* Free template text array */
  303.  
  304.    VOID
  305. free_template_text_array(struct Template  *tp)
  306. {
  307.    struct GadgetData  *gd = &tp->tp_Data.tp_GadgetData;
  308.    BYTE **text_array;
  309.  
  310.    switch (tp->tp_Type) {
  311.       case TEMPLATE_TYPE_MX :
  312.      text_array = gd->gd_SpecialData.gd_MXData.gd_MXTextArray;
  313.      gd->gd_SpecialData.gd_MXData.gd_MXTextArray = NULL;
  314.      break;
  315.       case TEMPLATE_TYPE_CYCLE :
  316.      text_array = gd->gd_SpecialData.gd_CycleData.gd_CycleTextArray;
  317.      gd->gd_SpecialData.gd_CycleData.gd_CycleTextArray = NULL;
  318.      break;
  319.       default :
  320.      text_array = NULL;
  321.      break;
  322.    }
  323.    DosFreeMem(text_array);
  324. }
  325.